package com.ethink.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.order.api.constant.OrderRespCode;
import com.ethink.order.api.enums.TocSaleReturnImportLogOrderStatusEnum;
import com.ethink.order.api.info.TocSaleReturnImportLogDetailInfo;
import com.ethink.order.api.info.TocSaleReturnImportLogInfo;
import com.ethink.order.api.model.TocSaleReturnImportModel;
import com.ethink.order.api.request.TocSaleReturnImportLogAddRequest;
import com.ethink.order.api.request.TocSaleReturnImportLogDetailAddRequest;
import com.ethink.order.api.request.TocSaleReturnImportLogQueryRequest;
import com.ethink.order.api.request.UpdateStatusBaseRequest;
import com.ethink.order.api.response.TocSaleReturnImportLogDetailResponse;
import com.ethink.order.common.entity.TocSaleReturnImportLog;
import com.ethink.order.common.entity.TocSaleReturnImportLogDetail;
import com.ethink.order.common.mapper.TocSaleReturnImportLogDetailMapper;
import com.ethink.order.common.mapper.TocSaleReturnImportLogMapper;
import com.ethink.order.service.TocSaleReturnImportLogDetailService;
import com.ethink.order.service.TocSaleReturnImportLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * TOC销售退单导入日志 Service接口实现
 *
 * @author default
 * @date 2022-12-16
 */
@Slf4j
@Service
public class TocSaleReturnImportLogServiceImpl implements TocSaleReturnImportLogService {

    @Autowired
    private TocSaleReturnImportLogMapper tocSaleReturnImportLogMapper;

    @Autowired
    private TocSaleReturnImportLogDetailMapper tocSaleReturnImportLogDetailMapper;

    @Autowired
    private TocSaleReturnImportLogDetailService tocSaleReturnImportLogDetailService;

    @Override
    public Integer add(TocSaleReturnImportLogAddRequest request) {
        List<String> shopOrderNoList = request.getItem().stream().map(TocSaleReturnImportLogDetailAddRequest::getShopOrderNo).distinct()
                .collect(Collectors.toList());
        List<TocSaleReturnImportLogDetailInfo> detailInfos = tocSaleReturnImportLogDetailMapper.selectListByChannelAndShopNo(request.getSaleSubChannel(), shopOrderNoList);
        Map<String, TocSaleReturnImportLogDetailInfo> detailInfoMap = detailInfos.stream().collect(Collectors.toMap(TocSaleReturnImportLogDetailInfo::getShopOrderNo, Function.identity()));
        TocSaleReturnImportLog tocSaleReturnImportLog = BeanUtil.copyProperties(request, TocSaleReturnImportLog.class, "item");
        if (detailInfos.size() < request.getItem().size()) {
            tocSaleReturnImportLogMapper.insert(tocSaleReturnImportLog);
        }
        tocSaleReturnImportLogDetailService.batchInsert(request.getItem(), tocSaleReturnImportLog.getId(), detailInfoMap);
        return 1;
    }

    @Override
    public Page<TocSaleReturnImportModel> pageQuery(TocSaleReturnImportLogQueryRequest request) {
        return tocSaleReturnImportLogMapper.pageQuery(PageUtils.buildPage(request), request);
    }

    @Override
    public Integer delete(Long id) {
        TocSaleReturnImportLog tocSaleReturnImportLog = tocSaleReturnImportLogMapper.selectById(id);
        if (!TocSaleReturnImportLogOrderStatusEnum.S1.getIntValue().equals(tocSaleReturnImportLog.getOrderStatus())) {
            throw new BusinessException(OrderRespCode.SALE_ORDER_DETAIL_STATUS_IS_NOT_MET);
        }
        tocSaleReturnImportLogDetailMapper.update(null,
                new LambdaUpdateWrapper<TocSaleReturnImportLogDetail>()
                        .eq(TocSaleReturnImportLogDetail::getLogId, tocSaleReturnImportLog.getId())
                        .set(TocSaleReturnImportLogDetail::getIsDeleted, true));
        return tocSaleReturnImportLogMapper.delById(id);

    }

    @Override
    public TocSaleReturnImportLogDetailResponse queryDetail(Long id) {
        TocSaleReturnImportLogDetailResponse response = new TocSaleReturnImportLogDetailResponse();
        TocSaleReturnImportLog tocSaleReturnImportLog = tocSaleReturnImportLogMapper.selectById(id);
        if (ObjectUtil.isEmpty(tocSaleReturnImportLog)) {
            return response;
        }
        TocSaleReturnImportLogInfo tocSaleReturnImportLogInfo = BeanUtil.copyProperties(tocSaleReturnImportLog, TocSaleReturnImportLogInfo.class);
        response.setMain(tocSaleReturnImportLogInfo);
        List<TocSaleReturnImportLogDetailInfo> item = tocSaleReturnImportLogDetailMapper.selectListByLogId(tocSaleReturnImportLog.getId());
        response.setItem(item);
        return response;
    }

    @Override
    public Integer updateStatus(UpdateStatusBaseRequest request) {
        return tocSaleReturnImportLogMapper.update(null, new LambdaUpdateWrapper<TocSaleReturnImportLog>()
                .eq(TocSaleReturnImportLog::getId, request.getId())
                .set(TocSaleReturnImportLog::getOrderStatus, request.getOrderStatus())
        );
    }


}
